package us.wili.dev.client.service.impl;

import com.alibaba.fastjson.JSON;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.wili.dev.client.dao.ClientOrderMapper;
import us.wili.dev.client.model.Detail;
import us.wili.dev.client.model.req.AddOrderReq;
import us.wili.dev.client.model.req.OrderReq;
import us.wili.dev.client.model.rsp.OrderDetaRsp;
import us.wili.dev.client.model.rsp.OrderRsp;
import us.wili.dev.client.model.rsp.ShopcartRsp;
import us.wili.dev.client.service.ClientGoodsService;
import us.wili.dev.client.service.ClientOrderService;
import us.wili.dev.client.service.ClientUserService;
import us.wili.dev.model.entity.Goods;
import us.wili.dev.model.entity.Order;
import us.wili.dev.model.entity.User;
import us.wili.dev.model.example.OrderExample;
import us.wili.dev.service.impl.BaseOrderServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClientOrderServiceImpl extends BaseOrderServiceImpl implements ClientOrderService {
    @Resource
    private ClientOrderMapper clientOrderMapper;

    @Autowired
    private ClientUserService clientUserService;

    @Autowired
    private ClientGoodsService clientGoodsService;

    @Override
    public List<OrderRsp> myorder(OrderReq req) {
        User user = clientUserService.findUser(req.getUserID());
        List<Order> orders = searmyorder(user);
        List<OrderRsp> rsp = new ArrayList<>();
        for (Order order : orders) {
            List<Detail> detail = JSON.parseArray(order.getGoods(), Detail.class);
            for (Detail detail1 : detail) {
                OrderRsp rsp1 = OrderRsp.ToMyOrderRsp(order,detail1);
                rsp.add(rsp1);
            }
        }
        return rsp;
    }

    @Override
    public List<ShopcartRsp> WaitingForTheBuy(Long id) {
        User user = clientUserService.findUser(id);
        List<Order> orders = SearWaitingForTheBuy(user);
        List<ShopcartRsp> rsp = new ArrayList<>();
        for (Order order : orders) {
            Goods goods = clientGoodsService.ordergoods(order);
            ShopcartRsp rsp1 = ShopcartRsp.ToOrder(goods,order);
            rsp.add(rsp1);
        }
        return rsp;
    }

    @Override
    public List<ShopcartRsp> WaitingForThedelivery(Long id) {
        User user = clientUserService.findUser(id);
        List<Order> orders = SearWaitingForThedelivery(user);
        List<ShopcartRsp> rsp = new ArrayList<>();
        for (Order order : orders) {
            Goods goods = clientGoodsService.ordergoods(order);
            ShopcartRsp rsp1 = ShopcartRsp.ToOrder(goods,order);
            rsp.add(rsp1);
        }
        return rsp;
    }

    @Override
    public List<ShopcartRsp> WaitingForTheclosed(Long id) {
        User user = clientUserService.findUser(id);
        List<Order> orders = SearWaitingForTheclosed(user);
        List<ShopcartRsp> rsp = new ArrayList<>();
        for (Order order : orders) {
            Goods goods = clientGoodsService.ordergoods(order);
            ShopcartRsp rsp1 = ShopcartRsp.ToOrder(goods,order);
            rsp.add(rsp1);
        }
        return rsp;
    }

    @Override
    public OrderDetaRsp WaitingForTheBuydeta(Long id) {
        Order order = findOrder(id);
        return OrderDetaRsp.orderDetaRsp(order);
    }

    @Override
    public void deleteorder(Long id) {
        OrderExample example = new OrderExample();
        example.createCriteria().andIdEqualTo(id);
        clientOrderMapper.deleteByExample(example);
    }

    @Override
    public Order insertorder(AddOrderReq req) {
        Order order = new Order();
        order.setOrderTotalprice(req.getTotalprice());
        order.setShipname(req.getShipname());
        order.setShipphone(req.getShipphone());
        order.setAddress(req.getAddress());
        order.setUserid(req.getUserid());
        order.setGoods(req.getDetail());
        clientOrderMapper.insert(order);
        return order;
    }


    public Order findOrder(Long id) {
        OrderExample example = new OrderExample();
        example.createCriteria().andIdEqualTo(id);
        return clientOrderMapper.selectOneByExample(example);
    }

    /**
     * 所有订单
     * @param user
     * @return
     */
    public List<Order> searmyorder(User user){
        OrderExample example = new OrderExample();
        example.createCriteria().andUseridEqualTo(user.getId());
        return clientOrderMapper.selectByExample(example);
    }


    /**
     * 待付款
     * @param user
     * @return
     */
    public List<Order> SearWaitingForTheBuy(User user){
        long num = 1;
        OrderExample example = new OrderExample();
        example.createCriteria().andUseridEqualTo(user.getId()).andTypeEqualTo(num);
        return clientOrderMapper.selectByExample(example);
    }


    /**
     * 待发货
     */
    public List<Order> SearWaitingForThedelivery(User user){
        long num = 2;
        OrderExample example = new OrderExample();
        example.createCriteria().andUseridEqualTo(user.getId()).andTypeEqualTo(num);
        return clientOrderMapper.selectByExample(example);
    }

    /**
     * 待收货
     */
    public List<Order> SearWaitingForTheclosed(User user){
        long num = 3;
        OrderExample example = new OrderExample();
        example.createCriteria().andUseridEqualTo(user.getId()).andTypeEqualTo(num);
        return clientOrderMapper.selectByExample(example);
    }
}
